/**********************************************************************************
 * Copyright (c) 2012 - 2012,  TestBoneCP, All right reserved
 * Title: com.adc.db.util  PortertiesUtil.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-6-28 下午9:18:59 
 *********************************************************************************/

package com.adc.quicktools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;

import com.adc.quicktools.StringUtil;

public class PropertiesUtil {
	/**
	 * 定义properties的后缀；
	 */
	private final static String PROPERTOES_SUFFIX = ".properties";

	/**
	 * 
	 * Description: 加载指定路径的配置文件，相对根目录，可通过FileControl进行文件搜索
	 * author: shen 
	 * @param name 
	 * @return Properties 
	 */
	public static Properties loadProperties(String name) {
		Properties properties = null;
		InputStream inputStream = null;
		if (StringUtil.checkStringValue(name)) {
			if (!name.contains(PROPERTOES_SUFFIX)) {
				name = name.concat(PROPERTOES_SUFFIX);
			}
			if (name.contains(File.pathSeparator)) {
				if (new File(name).isFile()) {
					try {
						inputStream = new FileInputStream(name);
					} catch (IOException e) {
						e.printStackTrace();
					} 
				}
			}else {
				inputStream=PropertiesUtil.class.getClassLoader().getResourceAsStream(name);
			}
			properties = new Properties();
			try {
				properties.load(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return properties;
	}
	
	/**
	 * 
	 * Description: 将第一个properties的全部变量拷贝给第二个properties变量，
	 * 若第二个变量为空，则自动创建一个变量，并将第一个赋给新创建的变量；
	 * author:  shen
	 * @param from
	 * @param to
	 * @return Properties
	 */
	@SuppressWarnings("rawtypes")
	public static Properties copyProperties(Properties from,Properties to) {
		Properties result =null;
		if (from!=null) {
			result = new Properties(from);
			if (to == null) {
				to = new Properties(from);
			}
			for (Iterator it = from.entrySet().iterator(); it.hasNext();) {
				Entry entry = (Entry) it.next();
				to.put(entry.getKey(), entry.getValue());
			}
			result=to;
		}
		return result;
	}
	
	/**
	 * 
	 * Description: 将第一个properties追加到第二个变量中，若同名，根据第三个变量判断是否覆盖；
	 * 注意：这个方法在强制追加时会将properties中的非string对象调用toString方法； 
	 * author:  shen
	 * @param target 
	 * @param addTo 
	 * @param isforce 是否强制覆盖
	 * @return Properties
	 */
	@SuppressWarnings("rawtypes")
	public static Properties addProperties(Properties target,Properties addTo,boolean isforce) {
		Properties result=null;
		if (target!=null) {
			if (addTo!=null) {
				result=copyProperties(addTo, null);
				if (result!=null) {
					for (Iterator it = target.entrySet().iterator(); it.hasNext();) {
						Entry entry = (Entry) it.next();
						if (result.containsKey(entry.getKey())) {
							if (isforce) {
								result.setProperty(entry.getKey().toString(), entry.getValue().toString());
							}
						}else {
							result.put(entry.getKey(), entry.getValue());
						}
					}
				}
			}else { 
				result=copyProperties(target, addTo);
			}
		}
		return result;
	}


}
